﻿var crudServiceBaseUrl = "/WS/hdservice.aspx", dataSourcegrid, kcpdataSourcegrid;
var rowxnklcmd, rowkcpcmd;
function toDate(value) {
    var dateRegExp = /^\/Date\((.*?)\)\/$/;
    var date = dateRegExp.exec(value);
    return new Date(parseInt(date[1]));
}

$(document).ready(function () {

    rowxnklcmd = new Array();
    rowkcpcmd = new Array();
    if (contractpermistion >= 7) {
        rowkcpcmd.push({ name: "Delete", text: "Xóa", click: DeleteKCP }, { name: "Detail", text: "Chi tiet", click: AddEditKCP });
        rowxnklcmd.push({ name: "Delete", text: "Xóa", click: DeleteXNKL });
    }
    else {
                if (contractpermistion >= 1) {

        } else {
            //ko co quyen xem
            return;
        }
    }


    //XNKL Grid ----------------------------------------------------------------------------------------------------------//////////////////////////////
    dataSourcegrid = {
        transport: {
            read: {
                url: crudServiceBaseUrl + "/ListXNKL",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                data: {
                    "MaHD": MaHD
                }
            },
            update: {
                url: crudServiceBaseUrl + "/UpdateXNKL",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                complete: function (e) {
                    showMessage('success', "Cập nhật thành công");
                    $("#xnklgrid").data("kendoGrid").dataSource.read();
                },
                error: function (e) {
                    alert('error');
                }
            },
            destroy: {
                url: crudServiceBaseUrl + "/DestroyXNKL",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                complete: function (e) {
                    showMessage('success', "Xóa thành công");
                    $("#xnklgrid").data("kendoGrid").dataSource.read();
                },
                error: function (e) {
                    alert('error');
                }
            },
            create: {
                url: crudServiceBaseUrl + "/CreateXNKL",
                contentType: "application/json; charset=utf-8",
                type: "POST",
                complete: function (e) {
                    showMessage('success', "Cập nhật thành công");
                    $("#xnklgrid").data("kendoGrid").dataSource.read();
                },
                error: function (e) {
                    alert('error');
                }

            },
            parameterMap: function (data, operation) {
                if (operation != "read") {
                    console.log(data);
                    return JSON.stringify({ obj: data.models })
                }
                else {
                    console.log(data);
                    data = $.extend({ Keyword: MaHD }, data);
                    return JSON.stringify({ obj: data });
                }
            }
        },
        batch: true,
        pageSize: 10,
        serverPaging: true,
        schema: {
            data: function (data) {
                console.log(data);
                return data.d;
            },
            total: function (data) {
                return data.d.length;
            },
            model: {
                id: "MaXNKL",
                fields: {
                    MaHD: { defaultValue: MaHD, editable: false },
                    NoiDung: { validation: { required: true} },
                    GiaTri: { type: "number", validation: { required: true} },
                    SoHoaDon: { validation: { required: false} },
                    NgayXNKL: { validation: { required: false} }
                }
            }
        }
    };

    $("#xnklgrid").kendoGrid({
        dataSource: dataSourcegrid,
        toolbar: kendo.template($("#template").html()),
        pageable: true,
        resizable: true,
        navigatable: true,
        //batch: true,
        columns: [
                            { field: "NoiDung", title: "Nội dung", width: 200, editor: textEditorInitialize },
                            { field: "GiaTri", title: "Giá trị", width: 110, format: "{0:N}" },
                            { field: "SoHoaDon", title: "Số hóa đơn", width: 110 },
                            { field: "NgayXNKL", width: 110, type: "date", format: "{0:dd/MM/yyyy}", editor: columndatepicker },
                            { field: "GhiChu", title: "Ghi chú", width: 200, editor: textEditorInitialize },
                            { command: rowxnklcmd, title: "", width: (rowxnklcmd.length * 60) + "px" }
                            ],

        editable: contractpermistion >= 3
    });

    //End XNKL ------------------------------------------------------------------------------------------------------------------------------////////////

    //KCP grid ------------------------------------------------------------------------------------------------------------------------------////////////
//    kcpdataSourcegrid = {
//        transport: {
//            read: {
//                url: crudServiceBaseUrl + "/ListKCP",
//                contentType: "application/json; charset=utf-8",
//                type: "POST"
//            },
//            update: {
//                url: crudServiceBaseUrl + "/UpdateKCP",
//                contentType: "application/json; charset=utf-8",
//                type: "POST",
//                complete: function (e) {
//                    showMessage('success', "Cập nhật thành công");
//                    $("#kcpgrid").data("kendoGrid").dataSource.read();
//                },
//                error: function (e) {
//                    alert('error');
//                }
//            },
//            destroy: {
//                url: crudServiceBaseUrl + "/DestroyKCP",
//                dataType: "jsonp"
//            },
//            create: {
//                url: crudServiceBaseUrl + "/CreateKCP",
//                contentType: "application/json; charset=utf-8",
//                type: "POST",
//                complete: function (e) {
//                    showMessage('success', "Cập nhật thành công");
//                    $("#kcpgrid").data("kendoGrid").dataSource.read();
//                },
//                error: function (e) {
//                    alert('error');
//                }

//            },
//            parameterMap: function (data, operation) {
//                if (operation != "read") {
//                    console.log(data);
//                    return JSON.stringify({ obj: data.models })
//                }
//                else {
//                    console.log(data);
//                    data = $.extend({ Keyword: MaHD }, data);
//                    return JSON.stringify({ obj: data });
//                }
//            }
//        },
//        batch: true,
//        pageSize: 10,
//        serverPaging: true,
//        schema: {
//            data: function (data) {
//                console.log(data);
//                return data.d;
//            },
//            total: function (data) {
//                return data.d.length;
//            },
//            model: {
//                id: "MaKCP",
//                fields: {
//                    MaHD: { defaultValue: MaHD, editable: false },
//                    SoKCP: { editable: false },
//                    GiaTri: { editable: false },
//                    Ngay: { editable: false },
//                    GhiChu: { editable: false },
//                    CPGK_TTTC: { editable: false },
//                    CPGK_QuaCTy: { editable: false },
//                    CPGK_TKCM: { editable: false },
//                    CPGK_Luong: { editable: false },
//                    CPGK_CPK: { editable: false }
//                }
//            }
//        }
//    };


//    $("#kcpgrid").kendoGrid({
//        dataSource: kcpdataSourcegrid,
//        toolbar: kendo.template($("#ckptemplate ").html()),
//        pageable: true,
//        resizable: true,
//        navigatable: true,
//        //batch: true,
//        columns: [
//                            { field: "SoKCP", title: "Số", width: 110 },
//                            { field: "GiaTri", title: "Giá trị", width: 110, format: "{0:N}" },
//                            { field: "Ngay", width: 110, type: "date", format: "{0:dd/MM/yyyy}", editor: columndatepicker, width: "110px" },
//                            { field: "GhiChu", title: "Ghi chú", width: 110, editor: textEditorInitialize },
//                            { field: "CPGK_TTTC", title: "TTTC", width: 110, format: "{0:N}" },
//                            { field: "CPGK_QuaCTy", title: "Chi qua cty", width: 110, format: "{0:N}" },
//                            { field: "CPGK_TKCM", title: "TKCM", width: 110, format: "{0:N}" },
//                            { field: "CPGK_Luong", title: "Lương", width: 110, format: "{0:N}" },
//                            { field: "CPGK_CPK", title: "Chi phí khác", width: 110, format: "{0:N}" },
//                            { command: rowkcpcmd, title: " ", width: (rowkcpcmd.length * 60) + "px" }
//                            ],

//        editable: contractpermistion >= 3
//    });
    //End KCP grid---------------------------------------------------------------------------------------------------------------------///////
});

function DeleteXNKL(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var mahddelete = dataItem.MaXNKL;
    if (confirm("Bạn có chắc chắn muốn xóa không?")) {
        $.post("/Handlers/post.ashx", { MaXNKL: mahddelete, act: "xxnkl" }, function (result) {
            if (result == "success") {
                var grid = $('#xnklgrid').data('kendoGrid');
                grid.dataSource.remove(dataItem);
                showMessage('success', "Xóa thành công!");
            } else {
                showMessage('error', "Xóa không thành công!");
            }

        });
    }
}

function DeleteKCP(e) {
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var mahddelete = dataItem.MaKCP;
    if (confirm("Bạn có chắc chắn muốn xóa không?")) {
        $.post("/Handlers/post.ashx", { MaKCP: mahddelete, act: "xkcp" }, function (result) {
            if (result == "success") {
                var grid = $('#kcpgrid').data('kendoGrid');
                grid.dataSource.remove(dataItem);
                showMessage('success', "Xóa thành công!");
            } else {
                showMessage('error', "Xóa không thành công!");
            }

        });
    }
}
var popupwindow;
function AddEditKCP(e) {
    var MaKCP = 0;
    if (e != null) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        MaKCP = dataItem.MaKCP;
    }
    popupwindow = $("#window");
    popupwindow.kendoWindow({
        width: "750px",
        height: "600px",
        title: "Thêm/Sửa Khoán chi phí",
        content: "/KCPPopup.aspx?MaHD=" + MaHD + "&MaKCP=" + MaKCP,
        modal: true,
        animation: false,
        center: true
    });
    popupwindow.data("kendoWindow").open().center();
}
